package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbjfxPartsrepair;
import com.jy.adscm.operation.mscyb.service.JqbjfxPartsrepairService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-12-10
 */
@RestController
@RequestMapping("/adscm/operation/jqbjfxPartsrepair")
public class JqbjfxPartsrepairController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqbjfxPartsrepairService jqbjfxPartsrepairService;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqbjfxPartsrepair jqbjfxPartsrepair, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbjfxPartsrepair.setTaskreleaseperson(token.getUser().getId().toString());
        jqbjfxPartsrepair.setTaskreleaseaddtime(new Date());
        jqbjfxPartsrepair.setTaskfinishstate(0);
        jqbjfxPartsrepair.setTaskstate(0);
        jqbjfxPartsrepair.setIstaskstart("0");
        jqbjfxPartsrepair.setIstaskbegin("0");
        jqbjfxPartsrepair.setIstaskend("0");
        jqbjfxPartsrepair.setIsreviewtask("0");
        jqbjfxPartsrepair.setReviewresult("-1");
        boolean insert = jqbjfxPartsrepairService.insertJqbjfxPartsrepair(jqbjfxPartsrepair);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException {
        boolean result = jqbjfxPartsrepairService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqbjfxPartsrepair jqbjfxPartsrepair, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = jqbjfxPartsrepairService.updateJqbjfxPartsrepair(jqbjfxPartsrepair,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectJqbjfxPartsrepair/{taskState}/{taskFinishState}/{isReviewTask}")
    public  SimpleCoreResponse  selectLjzzPartsmade(@PathVariable Integer taskState ,@PathVariable Integer taskFinishState,@PathVariable Integer isReviewTask) throws  AdscmSystemException{
        List<JqbjfxPartsrepair> jqbjfxPartsrepairs = jqbjfxPartsrepairService.selectJqbjfxPartsrepair(taskState,taskFinishState,isReviewTask);
        return  new SimpleCoreResponse("200","查询成功",jqbjfxPartsrepairs);
    }

    @RequestMapping("selectTaskPerformPerson")
    public  SimpleCoreResponse  selectTaskPerformPerson() throws  AdscmSystemException{
        List<JyUser> users = jqbjfxPartsrepairService.selectTaskPerformPerson();
        return  new SimpleCoreResponse("200","查询成功",users);
    }
    @RequestMapping("selectJqMbParts")
    public  SimpleCoreResponse  selectJqMbParts() throws  AdscmSystemException{
        Wrapper<JyCode> wrapper =new EntityWrapper<>();
        wrapper.eq("code_type","1");
        List<JyCode> jyCodes = jyCodeMapper.selectList(wrapper);
        return  new SimpleCoreResponse("200","查询成功",jyCodes);
    }
}

